﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using FlexCell;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using HyMaterialStatistic;

namespace PlumbingMaterialStatistic
{
	internal class PlumbingEquipNumNameList
	{
		public PlumbingEquipNumNameList(Autodesk.Revit.DB.Document doc, List<Dictionary<int, SatatisticDataInfo>> lstDicElement, string gridName, GridSetInfo gridSetInfo, ref FlexCell.Grid grid)
		{
			this.m_doc = doc;
			this.m_grid = grid;
			this.m_lstDicElems = lstDicElement;
			this.m_strGridName = gridName;
			this.m_gridSetInfo = gridSetInfo;
		}

		public bool GetStatisticGrid()
		{
			if (this.m_lstDicElems.Count <= 0 || this.m_gridSetInfo == null)
			{
				return false;
			}
			this.GetStatisticData();
			this.m_grid.Locked = false;
			if (!this.m_gridSetInfo.TitleDown)
			{
				if (!this.SetDataToGridUp())
				{
					return false;
				}
			}
			else if (!this.SetDataToGridDown())
			{
				return false;
			}
			return true;
		}

		private void GetStatisticData()
		{
			if (this.m_doc == null)
			{
				return;
			}
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstDicElems.Count, "正在初始化设备编号名称对照表中的数据", ProgressBarStyle.Blocks, true, false, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			int num = 1;
			foreach (Dictionary<int, SatatisticDataInfo> dictionary in this.m_lstDicElems)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					break;
				}
				if (dictionary.Count > 0)
				{
					Dictionary<int, List<string>> dictionary2 = new Dictionary<int, List<string>>();
					Dictionary<int, List<string>> dictionary3 = new Dictionary<int, List<string>>();
					foreach (KeyValuePair<int, SatatisticDataInfo> keyValuePair in dictionary)
					{
						ElementId elementId = new ElementId(keyValuePair.Key);
						Element element = this.m_doc.GetElement(elementId);
						if (element is FamilyInstance)
						{
							FamilyInstance familyInstance = element as FamilyInstance;
							List<string> list = new List<string>();
							List<string> list2 = new List<string>();
							list.Insert(0, num.ToString());
							string name = familyInstance.Symbol.Family.Name;
							list2.Add(name);
							string item = "Q=" + familyInstance.Symbol.GetParameter("流量").AsValueString();
							list2.Add(item);
							double dLength = familyInstance.Symbol.GetParameter("扬程").AsDouble();
							string item2 = "H" + (UnitConvert.LengthConverFromAPI(this.m_doc, dLength) / 1000.0).ToString("0.00") + "m";
							list2.Add(item2);
							double value = familyInstance.Symbol.GetParameter("电机功率").AsDouble();
                            string item3 = "N=" + (UnitConvert.CovertFromAPI((DisplayUnitType)84, value) / 1000.0).ToString("0.00") + "KW";
							list2.Add(item3);
							string item4 = "n=" + familyInstance.Symbol.GetParameter("转速").AsString();
							list2.Add(item4);
							list.Insert(1, "");
							list.Insert(2, keyValuePair.Value.Unit);
							string text = Math.Ceiling(keyValuePair.Value.Amount * (1.0 + (double)this.m_gridSetInfo.Margin / 100.0)).ToString("0.00");
							if (text.IndexOf('.') != -1)
							{
								text = text.TrimEnd(new char[]
								{
									'0'
								});
								text = text.Trim(new char[]
								{
									'.'
								});
							}
							list.Insert(3, text);
							int key = -1;
							if (this.ElemExsit(list2, dictionary3, out key))
							{
								string text2 = (Convert.ToDouble(dictionary2[key][3]) + keyValuePair.Value.Amount).ToString("0.00");
								if (text2.IndexOf('.') != -1)
								{
									text2 = text2.TrimEnd(new char[]
									{
										'0'
									});
									text2 = text2.Trim(new char[]
									{
										'.'
									});
								}
								dictionary2[key][3] = text.ToString();
							}
							else
							{
								dictionary2.Add(num, list);
								dictionary3.Add(num, list2);
							}
							num++;
						}
					}
					this.m_lstDicData.Add(dictionary2);
					this.m_lstDicParm2.Add(dictionary3);
				}
			}
		}

		private bool ElemExsit(List<string> lstParm, Dictionary<int, List<string>> dicParm2, out int key)
		{
			foreach (int num in dicParm2.Keys)
			{
				if (dicParm2[num].Except(lstParm).Count<string>() <= 0)
				{
					key = num;
					return true;
				}
			}
			key = -1;
			return false;
		}

		private bool SetDataToGridUp()
		{
			if (this.m_lstDicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			this.m_grid.Cell(1, 1).Text = this.m_strGridName;
			this.m_grid.Cell(1, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			int num = 3;
			this.m_grid.Range(num + 1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			int num2 = 0;
			foreach (Dictionary<int, List<string>> dictionary in this.m_lstDicData)
			{
				num2 += dictionary.Count;
			}
			num2 *= 3;
			this.m_grid.Rows = num + num2;
			for (int i = 0; i < this.m_lstDicData.Count; i++)
			{
				Dictionary<int, List<string>> dictionary2 = this.m_lstDicData[i];
				Dictionary<int, List<string>> dictionary3 = this.m_lstDicParm2[i];
				foreach (KeyValuePair<int, List<string>> keyValuePair in dictionary2)
				{
					List<string> value = keyValuePair.Value;
					if (value.Count > 0)
					{
						int num3 = num;
						for (int j = 0; j < value.Count; j++)
						{
							if (j == 1)
							{
								List<string> list = dictionary3[keyValuePair.Key];
								this.m_grid.Cell(num, 2).Text = list[0];
								this.m_grid.Cell(++num, 2).Text = list[1] + "," + list[2];
								this.m_grid.Cell(++num, 2).Text = list[3] + "," + list[4];
							}
							else
							{
								this.m_grid.Cell(num3, j + 1).Text = value[j];
								this.m_grid.Cell(num3, j + 1).Alignment = AlignmentEnum.CenterCenter;
							}
						}
						this.m_grid.Range(num3, 1, num, 1).Merge();
						this.m_grid.Range(num3, 3, num, 3).Merge();
						this.m_grid.Range(num3, 4, num, 4).Merge();
						num++;
					}
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			for (int k = 1; k < this.m_grid.Rows; k++)
			{
				this.m_grid.Row(k).AutoFit();
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private bool SetDataToGridDown()
		{
			if (this.m_lstDicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			int num = 0;
			foreach (Dictionary<int, List<string>> dictionary in this.m_lstDicData)
			{
				num += dictionary.Count;
			}
			num *= 3;
			this.m_grid.Rows = num + 3;
			this.m_grid.Cols = 5;
			this.DrawTitle(num);
			int num2 = num;
			for (int i = 0; i < this.m_lstDicData.Count; i++)
			{
				Dictionary<int, List<string>> dictionary2 = this.m_lstDicData[i];
				Dictionary<int, List<string>> dictionary3 = this.m_lstDicParm2[i];
				foreach (KeyValuePair<int, List<string>> keyValuePair in dictionary2)
				{
					List<string> value = keyValuePair.Value;
					if (value.Count > 0)
					{
						int num3 = num2;
						for (int j = 0; j < value.Count; j++)
						{
							if (j == 1)
							{
								List<string> list = dictionary3[keyValuePair.Key];
								this.m_grid.Cell(num2, 2).Text = list[0];
								this.m_grid.Cell(--num2, 2).Text = list[1] + "," + list[2];
								this.m_grid.Cell(--num2, 2).Text = list[3] + "," + list[4];
							}
							else
							{
								this.m_grid.Cell(num3, j + 1).Text = value[j];
								this.m_grid.Cell(num3, j + 1).Alignment = AlignmentEnum.CenterCenter;
							}
						}
						this.m_grid.Range(num3, 1, num2, 1).Merge();
						this.m_grid.Range(num3, 3, num2, 3).Merge();
						this.m_grid.Range(num3, 4, num2, 4).Merge();
						num2--;
					}
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Range(1, 1, num + 1, this.m_grid.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			this.m_grid.Range(1, 1, num + 1, this.m_grid.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			for (int k = 1; k < this.m_grid.Rows; k++)
			{
				this.m_grid.Row(k).AutoFit();
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private void DrawTitle(int nRows)
		{
			this.m_grid.Cell(nRows + 1, 1).Text = "编号";
			this.m_grid.Cell(nRows + 1, 2).Text = "用途及性能参数";
			this.m_grid.Cell(nRows + 1, 3).Text = "单位";
			this.m_grid.Cell(nRows + 1, 4).Text = "数量";
			this.m_grid.Cell(nRows + 2, 1).Text = this.m_strGridName;
			this.m_grid.Range(nRows + 2, 1, nRows + 2, 4).Merge();
			this.m_grid.Cell(nRows + 2, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			this.m_grid.Range(nRows + 1, 1, nRows + 2, 4).Alignment = AlignmentEnum.CenterCenter;
		}

		private Document m_doc;

		private List<Dictionary<int, SatatisticDataInfo>> m_lstDicElems;

		private FlexCell.Grid m_grid;

		private List<Dictionary<int, List<string>>> m_lstDicData = new List<Dictionary<int, List<string>>>();

		private List<Dictionary<int, List<string>>> m_lstDicParm2 = new List<Dictionary<int, List<string>>>();

		private string m_strGridName = "";

		private GridSetInfo m_gridSetInfo;
	}
}
